/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.worker.controller;

import org.ourgrid.common.interfaces.WorkerClient;
import org.ourgrid.common.interfaces.to.WorkAccounting;
import org.ourgrid.reqtrace.Req;
import org.ourgrid.worker.WorkerConstants;
import org.ourgrid.worker.dao.GridProcessDAO;
import org.ourgrid.worker.dao.WorkAccountingDAO;
import org.ourgrid.worker.dao.WorkerStatusDAO;

import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;

public class WorkerController {

	private static WorkerController instance = null;
	
	@Req("REQ079")
	public static synchronized WorkerController getInstance() {
		if (instance == null) {
			instance = new WorkerController();
		}
		return instance;
	}
	
	public void interruptWorking(ServiceManager serviceManager, boolean releaseRemotePeer) {
		cleanWorker(serviceManager, true, releaseRemotePeer);
		undeployWorkerExecutionClient(serviceManager);
	}
	
	public void cleanWorker(ServiceManager serviceManager) {
		cleanWorker(serviceManager, false, false);
	}
	
	public void cleanWorker(ServiceManager serviceManager, boolean releaseConsumer, boolean releaseRemotePeer) {
		WorkerStatusDAO workerStatusDAO = serviceManager.getDAO(WorkerStatusDAO.class);
		GridProcessDAO gridProcessDAO = serviceManager.getDAO(GridProcessDAO.class);
		
		if (workerStatusDAO.hasConsumer()) {
			
			if (releaseConsumer) {
				WorkerClient consumer = workerStatusDAO.getConsumer();
				serviceManager.release(consumer);
				workerStatusDAO.setConsumer(null);
			}
			
			if (workerStatusDAO.isAllocatedForRemotePeer() && !gridProcessDAO.isExecutionFinish()) {
				finishWorkAccounting(serviceManager);
			}
		}
		
		if (releaseConsumer && workerStatusDAO.isAllocatedForBroker()) {
			undeployWorker(serviceManager);
			workerStatusDAO.setConsumerPublicKey(null);
		}
		
		if (releaseRemotePeer && workerStatusDAO.isAllocatedForRemotePeer()) {
			workerStatusDAO.setCaCertificates(null);
			workerStatusDAO.setUsersDN(null);
			
			undeployRemoteWorkerManagement(serviceManager);
			workerStatusDAO.setRemotePeerPublicKey(null);
		}

		
		gridProcessDAO.setExecutionFinish(false);
		workerStatusDAO.setErrorState(false);
		
		ExecutionController.getInstance().cancelActiveExecution(serviceManager);
		FileTransferController.getInstance().cancelCurrentTransfers(serviceManager);
		EnvironmentController.getInstance().unmountEnvironment(serviceManager);
	}
	
	public void finishWorkAccounting(ServiceManager serviceManager) {
		WorkAccountingDAO accountingDAO = serviceManager.getDAO(WorkAccountingDAO.class);
		WorkAccounting accounting = accountingDAO.getActualWorkAccounting();
		accounting.stopCPUTiming();
		accountingDAO.addWorkAccounting(accounting);
		accountingDAO.setActualWorkAccounting(null);
	}
	
	private void undeployWorkerExecutionClient(ServiceManager serviceManager) {
		serviceManager.undeploy(WorkerConstants.WORKER_EXECUTION_CLIENT);		
	}
	
	private void undeployWorker(ServiceManager serviceManager) {
		serviceManager.undeploy(WorkerConstants.WORKER);
	}

	private void undeployRemoteWorkerManagement(ServiceManager serviceManager) {
		serviceManager.undeploy(WorkerConstants.REMOTE_WORKER_MANAGEMENT);
	}

}
